package com.dao.impl;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.dao.AreainfoDao;
import com.enetiy.Areainfo;

public class AreainfoDaoImpl extends HibernateDaoSupport implements AreainfoDao {

	@Override
	public List<Areainfo> selectAreainfo(int areaid) throws Exception {
		Query query = this.getSession().createQuery("from Areainfo where aeraid=?");
		query.setParameter(0, areaid);
		List<Areainfo> list = query.list();
		return list;
	}

	@Override
	public List<Areainfo> selectAreainfoIdenList(int iden,int areaid) throws Exception {
		String sql = "SELECT ai.* FROM areausable AS au ,areainfo AS ai  WHERE au.`areainfo_id` = ai.`id`AND ai.`iden`>? AND au.`area_id`=?";
		Query query = this.getSession().createSQLQuery(sql).addEntity(Areainfo.class);
		query.setParameter(0, iden);
		query.setParameter(1, areaid);
		List<Areainfo> list = query.list();
		return list;
	}

	@Override
	public List<Areainfo> selectAreainfoList(int areaid, String KeywordMohu)
			throws Exception {
		String sql = "SELECT ai.* FROM areausable AS au ,areainfo AS ai, areanet AS an  WHERE" +
				" au.`areainfo_id` = ai.`id` " +
				"AND ai.`iden`=an.`id` " +
				"AND ai.`aeraid`= ? " +
				"AND an.`keyword`LIKE ?;";
		Query query = this.getSession().createSQLQuery(sql).addEntity(Areainfo.class);
		query.setParameter(0, areaid);
		query.setParameter(1, KeywordMohu+"%");
		List<Areainfo> list = query.list();
		return list;
	}

	@Override
	public Areainfo selectAreainfo(int areaid, String Keyword) throws Exception {
		String sql = "SELECT ai.* FROM  areainfo AS ai, areanet AS an  WHERE " +
				" ai.`iden`= an.`id` " +
				"AND ai.`aeraid` = ? " +
				"AND an.`keyword`= ?;";
		Query query = this.getSession().createSQLQuery(sql).addEntity(Areainfo.class);
		query.setParameter(0, areaid);
		query.setParameter(1, Keyword);
		List<Areainfo> list = query.list();
		return list.isEmpty()?null:list.get(0);
	}
	
}
